// 防抖函数
export function debounce(fn, wait, immediate) {
  let timer

  return function() {
    let context = this
    let args = arguments

    if (timer) {
      clearTimeout(timer)
    }

    if (immediate) {
      let callNow = !timer
      timer = setTimeout(() => {
        timer = null
      }, wait)

      if (callNow) {
        fn.apply(context, args)
      }
    } else {
      timer = setTimeout(function() {
        fn.apply(context, args)
      }, wait)
    }
  }
}

// 验证是否外部地址
export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}

// 判断url最后是否指定参数
export function isUrlFinallyParameter(parameter) {
  const url = window.location.href
  const arr = url.split('/')
  return arr[arr.length - 1] === parameter
}
